
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import javax.swing.*;

public class MainFrame extends JFrame
{
    HashMap<Calendar,String[]> _appointments = new HashMap<>();
    Calendar c = Calendar.getInstance();
    private JPanel mainPanel;
    private JPanel northPanel;
    private JLabel lblMonthName;

    public MainFrame()
    {
        super("Calendar");
        northPanel = new JPanel();
        lblMonthName = new JLabel();
        final JButton btnPrev = new JButton("prev");
        btnPrev.addActionListener(
                new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        c.add(Calendar.MONTH, -1);
                        InitCalendar();

                    }
                });
        northPanel.add(btnPrev);
        northPanel.add(lblMonthName);
        final JButton btnNext = new JButton("next");
        btnNext.addActionListener(
                new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        c.add(Calendar.MONTH, 1);
                        InitCalendar();
                    }
                });
        northPanel.add(btnNext);
        add(northPanel,BorderLayout.NORTH);
        InitCalendar();
    }

    private void InitCalendar()
    {
        lblMonthName.setText(getMonthName(c) + " " + c.get(Calendar.YEAR));
        if(mainPanel != null)
            remove(mainPanel);
        c.set(c.get(Calendar.YEAR),c.get(Calendar.MONTH),1);
        int firstDayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        int daysOfMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        int numberOfRows = (daysOfMonth + firstDayOfWeek - 2) / 7 + 2;
        mainPanel = new JPanel(new GridLayout(numberOfRows,7,1,1));

        // add headers labels
        for(String s :new String[] {"א","ב","ג","ד","ה","ו","ש"})
        {
            final JLabel l = new JLabel(s);
            l.setHorizontalAlignment(SwingConstants.CENTER);
            mainPanel.add(l);
        }

        // create the first blank rubrics
        for(int i = 0; i < firstDayOfWeek-1; i++)
        {
            mainPanel.add(new JList());
        }

        // add the actual dates to the calendar
        for(int i = 0; i < daysOfMonth; i++)
        {
            DefaultListModel model = new DefaultListModel();
            model.add(0,i+1);

            if(_appointments.containsKey(c))
            {
                for(int j = 0; j < _appointments.get(c).length; j++)
                    model.add(j+1,_appointments.get(c)[j]);
            }

            final JList list = new JList(model);
            list.addMouseListener(
                    new MouseAdapter()
                    {
                        @Override
                        public void mouseClicked(MouseEvent e)
                        {
                            super.mouseClicked(e);
                            List<String> appointments = new ArrayList<>();
                            if(_appointments.containsKey(c))
                                    appointments =  Arrays.asList( _appointments.get(c));



                            Appointments appointmentsFrame = new Appointments(appointments);
                            appointmentsFrame.setSize(500, 500);
                            appointmentsFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                            appointmentsFrame.setLocationRelativeTo(null);
                            appointmentsFrame.setVisible(true);
                            _appointments.put(c, appointmentsFrame.getAppointments());
                        }
                    }
                );
            mainPanel.add(list);
        }

        // complete the left rubrics with blank lists
        for(int i = 0; i < 36-daysOfMonth-firstDayOfWeek ; i++)
        {
            mainPanel.add(new JList());
        }

        add(mainPanel,BorderLayout.CENTER);
        repaint();
    }

    // return the month name of the calendar parameter
    private String getMonthName(Calendar c)
    {
        String[] monthName = {"January", "February",
                                "March", "April", "May", "June", "July",
                                "August", "September", "October", "November",
                                "December"};
        return monthName[c.get(Calendar.MONTH)];
    }
}
